/*******************************************************************************
 * Copyright (c) 2013 Michele Nanna.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Michele Nanna - initial API and implementation
 ******************************************************************************/
package com.systemfastforward.provinciawifi;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockMapActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.cyrilmottier.polaris.Annotation;
import com.cyrilmottier.polaris.PolarisMapView;
import com.systemfastforward.provinciawifi.service.AccessPoint;
import com.systemfastforward.provinciawifi.service.AccessPointUtils;
import com.systemfastforward.provinciawifi.service.WebServiceProvinciaWifiCsvDownloader;

public class ListAccessPointActivity extends SherlockMapActivity {
	private PolarisMapView mMapView;
	private List<AccessPoint> mAccessPointList;
	private List<AccessPoint> mAccessPointListFiltered;
	private List<Annotation> mAccessPointAnnotationList;
	private String[] mComuniItems;
	private int mIndexComuneSelected = 0;
	private Builder mComuniChoosingDialog;
	private boolean menuItemComuniFilteringIsDisabled = true;
	private OnClickComuneListener onClickComuneListener = new OnClickComuneListener();
	private TextView mTvComuniFilter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.activity_list_access_point);

		// create dialog for list of comuni
		mComuniChoosingDialog = new AlertDialog.Builder(this);
		mComuniChoosingDialog.setTitle(getResources().getString(R.string.list_access_point_filter_comuni_title));
		mTvComuniFilter = (TextView) findViewById(R.id.list_access_point_filter_text);

		mMapView = (PolarisMapView) findViewById(R.id.list_access_point_map);
		mMapView.setUserTrackingButtonEnabled(true);

		new DownloadCsvTask().execute();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		boolean ret = super.onPrepareOptionsMenu(menu);
		menu.findItem(R.id.menu_filter).setEnabled(menuItemComuniFilteringIsDisabled);
		return ret;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_list_access_point, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i = null;
		switch (item.getItemId()) {
		case R.id.menu_social:
			i = new Intent(this, SocialActivity.class);
			startActivity(i);
			break;
		case R.id.menu_info:
			i = new Intent(this, InfoActivity.class);
			startActivity(i);
			break;
		case R.id.menu_filter:
			showComuniChoosingDialog();
			break;
		case R.id.menu_settings:
			i = new Intent(this, AutoLoginServicePreferenceActivity.class);
			startActivity(i);
			break;

		}
		return super.onOptionsItemSelected(item);
	}

	private void showComuniChoosingDialog() {
		mComuniChoosingDialog.setSingleChoiceItems(mComuniItems, mIndexComuneSelected, onClickComuneListener);
		mComuniChoosingDialog.show();
	}

	private class DownloadCsvTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			menuItemComuniFilteringIsDisabled = true;
			setProgressBarIndeterminateVisibility(true);
		}

		@Override
		protected Void doInBackground(Void... params) {
			// download accesspoint list
			mAccessPointList = WebServiceProvinciaWifiCsvDownloader.getInstanceFrom(getApplicationContext()).getAccessPointList();
			// create filtered list
			mAccessPointListFiltered = new ArrayList<AccessPoint>(mAccessPointList.size());
			mAccessPointListFiltered.addAll(mAccessPointList);
			// build annotation from accesspoint list
			mAccessPointAnnotationList = AccessPointUtils.buildAnnotationlistFromAccessPointList(mAccessPointListFiltered);
			// set comuni list, adding first element that represent no selection
			List<String> comuniRaw = AccessPointUtils.getComuniFromAccessPointList(mAccessPointList);
			comuniRaw.add(0, getResources().getString(R.string.list_access_point_all_comuni));
			mComuniItems = comuniRaw.toArray(new String[] {});
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			filterMapAnnotations();
			// disable-enable UI
			menuItemComuniFilteringIsDisabled = false;
			setProgressBarIndeterminateVisibility(false);
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void filterMapAnnotations() {

		// on selection of first element, all access point are visible
		if (mIndexComuneSelected == 0) {
			mAccessPointListFiltered.clear();
			mAccessPointListFiltered.addAll(mAccessPointList);
		} else {
			AccessPointUtils.filterAccessPointListByComune(mComuniItems[mIndexComuneSelected], mAccessPointList, mAccessPointListFiltered);
		}

		// set annotation
		mAccessPointAnnotationList = AccessPointUtils.buildAnnotationlistFromAccessPointList(mAccessPointListFiltered);
		mMapView.setAnnotations(mAccessPointAnnotationList, R.drawable.map_pin_holed_blue);

		// set filter text
		mTvComuniFilter.setText(getResources().getString(R.string.list_access_point_filter_text, mComuniItems[mIndexComuneSelected], mAccessPointAnnotationList.size()));
	}

	private class OnClickComuneListener implements DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			mIndexComuneSelected = which;
			filterMapAnnotations();
			dialog.dismiss();
		}
	}
}
